var DeleteFriend = function(userId1, userId2, options){
	this.userId1 = userId1;
	this.userId2 = userId2;
	this.error = options.error;
	this.success = options.success;
	this.delete();
};

DeleteFriend.prototype._success = function(data){
	if(typeof this.success == 'function') {
		this.success(data);
	}
};

DeleteFriend.prototype._error = function(code, message){
	if(typeof this.error == 'function') {
		this.error(code, message);
	}
};

DeleteFriend.prototype.check = function(nextStatement){
	var self = this;
	var where1 = '(userId1='+Mysql.escape(self.userId1)+' AND userId2='+Mysql.escape(self.userId2)+')';
	var where2 = '(userId2='+Mysql.escape(self.userId1)+' AND userId1='+Mysql.escape(self.userId2)+')';
	var where = where1+' OR '+where2;
	Mysql.query('SELECT COUNT(id) FROM Friend WHERE '+where+' LIMIT 1').on('error', function(error){
		console.log('Mysql error: '+error);
		self._error(9999, 'Mysql error');
	}).on('result', function(result){
		if(result['COUNT(id)'] == 0) {
			self._error(9999, 'Not found relation');
			return;
		}
		nextStatement();
	});
};

DeleteFriend.prototype.delete = function(){
	var self = this;
	self.check(function(){
		var where1 = '(userId1='+Mysql.escape(self.userId1)+' AND userId2='+Mysql.escape(self.userId2)+')';
		var where2 = '(userId2='+Mysql.escape(self.userId1)+' AND userId1='+Mysql.escape(self.userId2)+')';
		var where = where1+' OR '+where2;
		Mysql.query('DELETE FROM Friend WHERE '+where+' LIMIT 1').on('error', function(error){
			console.log('Mysql error: '+error);
			self._error(9999, 'Mysql error');
		})
		.on('result', function(result){
			self._success(result);
		});
	});
};

module.exports = DeleteFriend;